// import React2 from "react";


function useLoader({React}) {
  // const React = react||React2
  const [jsLoading, setJsLoading] = React.useState(false);
  const jsLoader = (jsUrl: string, load404: boolean = true) => {

    setJsLoading(true)
    return new Promise((resolve, reject) => {

      (window as any).requirejs(
        [jsUrl],
        (Component: any) => {
          setJsLoading(false)
          let ReactComponent = typeof Component === 'object' && Component.default ? Component.default : Component;
          
          resolve(ReactComponent);
       
        },
        (err: string) => {
          
          setJsLoading(false)
          reject(err)
          console.error('micro app loading failed:', err);
          //  if(load404){
          //      const jsUrl = joinUrl(publicPath, `/pages/404.js`);
          //      return jsLoader(jsUrl,publicPath,false).then(resolve)
          //  }else{
          //    reject()
          //  }
        },
      );
    });
  }
  const cssLoader = (url: string) => {
    let link = document.createElement('link');
    link.href = url
    link.rel = 'stylesheet'
    document.head.appendChild(link)

  }
  return {
    jsLoading,
    cssLoader,
    jsLoader
  }
}
export default useLoader